{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>U</th>\n",
       "      <th>M</th>\n",
       "      <th>L</th>\n",
       "      <th>T</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1027765</td>\n",
       "      <td>4822</td>\n",
       "      <td>172</td>\n",
       "      <td>20151028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>141398</td>\n",
       "      <td>4822</td>\n",
       "      <td>172</td>\n",
       "      <td>20151115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>141398</td>\n",
       "      <td>4822</td>\n",
       "      <td>172</td>\n",
       "      <td>20151005</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1027765</td>\n",
       "      <td>4822</td>\n",
       "      <td>172</td>\n",
       "      <td>20151026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>141398</td>\n",
       "      <td>4822</td>\n",
       "      <td>172</td>\n",
       "      <td>20151011</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         U     M    L         T\n",
       "0  1027765  4822  172  20151028\n",
       "1   141398  4822  172  20151115\n",
       "2   141398  4822  172  20151005\n",
       "3  1027765  4822  172  20151026\n",
       "4   141398  4822  172  20151011"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "sns.set(style=\"white\", color_codes=True)\n",
    "\n",
    "dat = pd.read_csv(\"icaj/ijcai2016_koubei_train\",  header=None, names=['U', 'M', 'L', 'T'])\n",
    "dat.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "X = dat.drop(\"M\", 1).drop(\"T\", 1).values\n",
    "y = dat[\"M\"].values\n",
    "\n",
    "from sklearn.cross_validation import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Run KNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn import neighbors, linear_model\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "knn  = neighbors.KNeighborsClassifier()\n",
    "# knn_model_1 = knn.fit(X_train, y_train)\n",
    "\n",
    "# print('k-NN accuracy for test set: %f' % knn_model_1.score(X_test, y_test))\n",
    "\n",
    "# y_true, y_pred = y_test, knn_model_1.predict(X_test)\n",
    "# print(classification_report(y_true, y_pred))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自定义KNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test = pd.read_csv('icaj/ijcai2016_koubei_test',  header=None, names=['U', 'M', 'L'])\n",
    "test.head()\n",
    "\n",
    "T = test.drop(\"L\", 1).values\n",
    "\n",
    "from collections import namedtuple\n",
    "from collections import Counter\n",
    "\n",
    "mp = {}\n",
    "mu = {}\n",
    "ml = {}\n",
    "\n",
    "for idx, d in enumerate(X):\n",
    "    k = str(d[0]) + \", \" + str(d[1])\n",
    "    if k not in mp:\n",
    "        mp[k] = Counter()\n",
    "    v = y[idx]\n",
    "    mp[k][v] += 1\n",
    "    if d[0] not in mu:\n",
    "        mu[d[0]] = Counter()\n",
    "    mu[d[0]][v] += 1\n",
    "    if d[1] not in ml:\n",
    "        ml[d[1]] = Counter()\n",
    "    ml[d[1]][v] +=1\n",
    "\n",
    "res = {}\n",
    "        \n",
    "for t in T:\n",
    "    k = str(t[0]) + \", \" + str(t[1])\n",
    " \n",
    "    if k in mp:\n",
    "        res[k] = mp[k].most_common(1)[0][0]\n",
    "    else:\n",
    "        res[k] = 0\n",
    "        if t[0] in mu:\n",
    "            k1 = mu[t[0]].most_common(1)[0]\n",
    "            if k1[1] > res[k]:\n",
    "                res[k] = k1[0]\n",
    "        if t[1] in ml:\n",
    "            k2 = ml[t[1]].most_common(1)[0]\n",
    "#             print(k2)\n",
    "            if k2[1] > res[k]:\n",
    "                res[k] = k2[0]\n",
    "#     break\n",
    "    \n",
    "# print(res)\n",
    "\n",
    "with open(\"res.csv\", \"w\") as fp:\n",
    "    for r in res:\n",
    "        fp.write(r + \", \" + str(res[r])  + \"\\n\")\n",
    "   \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
